//
//  ResumeViewController.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/22.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "ResumeViewController.h"
#import "ResumeHeaderV.h"
#import "BaseViewController.h"
#import "WorkExpViewController.h"
#import "EduViewController.h"
#import "HopViewController.h"

#import "UILabel+HWDUILabel.h"

#import "EduCell.h"
#import "EditCell.h"
#import "WorkMessageCell.h"
#import "WorkerCell.h"
#import "HopeCell.h"

@interface ResumeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableV;
@property (nonatomic,strong)NSDictionary *data;//数据

@property (nonatomic,strong)NSArray *titleArr;//标题
@property (nonatomic,strong)NSArray *BaseTitleArr;//标题
@end

@implementation ResumeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatView];
    [self getDetail];
}

-(void)creatView
{
    [self initTitleWithTitle:@"我的简历"];
    _titleArr = @[@"基本信息（必填）",@"工作经历（必填）",@"教育经历（必填）",@"期望工作（必填）",];
    _BaseTitleArr = @[@"姓      名",@"性      别",@"最高学历",@"工作年限",@"所在城市",@"联系电话",@"联系邮箱",@"一句话介绍"];
    [self.view addSubview:self.tableV];
}

-(UITableView *)tableV//创建View
{
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREENW, SCREENH-64) style:UITableViewStyleGrouped];
        _tableV.estimatedRowHeight = 150*screenW_6;
        _tableV.backgroundColor = backColor;
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.delegate = self;
        _tableV.dataSource = self;
    }
    return _tableV;
}
#pragma Mark TableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return _BaseTitleArr.count+1;
    }else if (section == 1){
        NSArray *arr = _data[@"jdUserWorkExperienceList"];
        return arr.count;
    }else if (section == 2){
        NSArray *arr = _data[@"jdUserLearningExperienceList"];
        return arr.count;
    }else if (section == 3){
        if ([NSString stringWithFormat:@"%@",_data[@"desiredPosition"]].length) {
            return 1;
        }
    }
    return 0;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 45*screenW_6;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.000001;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //区头
    ResumeHeaderV * headerV = [[ResumeHeaderV alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 50*screenW_6)];
    headerV.nameLab.text = _titleArr[section];
    [headerV.nameLab setHighlightStirng:@"（必填）" HighlightColor:TextGray];
    [headerV.nameLab setTextFont:@"（必填）" font:[UIFont systemFontOfSize:12*screenW_6]];
    headerV.chooseBtn.tag = section;
    UITapGestureRecognizer *headerTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headerSelectTap:)];
    [headerV.chooseBtn addGestureRecognizer:headerTap];
    return headerV;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {//基本信信
        if (indexPath.row <_BaseTitleArr.count) {
            EditCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EditCell"];
            if (!cell) {
                cell = [[EditCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"EditCell"];
            }
            NSString *placeHodel = @"";
            if (indexPath.row == 0) {
                placeHodel = _data[@"name"];
            }else if (indexPath.row == 1){
                placeHodel = [self getSex];
            }else if (indexPath.row == 2){
                placeHodel = [self getEducation];
            }else if (indexPath.row == 3){
                placeHodel = [self getWorkingLife];
            }else if (indexPath.row == 4){
                placeHodel = _data[@"city"];
            }else if (indexPath.row == 5){
                placeHodel = _data[@"contactTelephone"];
            }else if (indexPath.row == 6){
                placeHodel = _data[@"contactMailbox"];
            }
            [cell loadCell:_BaseTitleArr[indexPath.row] text:@"" placeHodel:placeHodel];
            cell.userInteractionEnabled = NO;
            return cell;
        }else
        {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
            }
            cell.selectionStyle = 0;
            cell.textLabel.text = [NSString stringWithFormat:@"%@",_data[@"introduce"]].length?_data[@"introduce"]:@"未填写";
            cell.textLabel.textColor = TextGray;
            cell.textLabel.numberOfLines = 0;
            cell.textLabel.font = [UIFont systemFontOfSize:14*screenW_6];
            [cell.textLabel setLineSpaceWithLineSpacing:8*screenW_6 text:cell.textLabel.text];
            [cell.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(cell.contentView).offset(20*screenW_6);
                make.top.equalTo(cell.contentView).offset(0);
                make.width.equalTo(@(SCREENW-50*screenW_6));
                make.bottom.equalTo(cell.contentView.mas_bottom).offset(-20*screenW_6);
            }];
            cell.userInteractionEnabled = NO;
            return cell;

        }
    }else if(indexPath.section == 1)//工作、教育经历、期望
    {
        WorkMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WorkMessageCell"];
        if (!cell) {
            cell = [[WorkMessageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"WorkMessageCell"];
        }
        NSArray *arr = [NSArray new];
        arr = _data[@"jdUserWorkExperienceList"];
        [cell setDataDic:arr[indexPath.row]];
        cell.userInteractionEnabled = NO;
        return cell;
    }else if (indexPath.section == 2){
        EduCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EduCell"];
        if (!cell) {
            cell = [[EduCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"EduCell"];
        }
        NSArray *arr = [NSArray new];
        arr = _data[@"jdUserLearningExperienceList"];
        [cell setDataDic:arr[indexPath.row]];
        cell.userInteractionEnabled = NO;
        return cell;
    }else if (indexPath.section == 3){
        HopeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HopeCell"];
        if (!cell) {
            cell = [[HopeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HopeCell"];
        }
        [cell setDataDic:_data];
        cell.userInteractionEnabled = NO;
        return cell;
    }
    return nil;
}
//区头点击事件
-(void)headerSelectTap:(UITapGestureRecognizer *)tap
{
    __weak typeof(self) weakSelf = self;
    if (tap.view.tag == 0) {//基本信息
        BaseViewController *vc = [BaseViewController new];
        vc.data = [NSMutableDictionary dictionaryWithDictionary:_data];
        vc.blcok = ^(NSDictionary *dic){
            weakSelf.data = dic;
            [weakSelf.tableV reloadData];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }else if (tap.view.tag == 1){//工作经历
        WorkExpViewController *vc = [WorkExpViewController new];
        vc.data = [NSMutableDictionary dictionaryWithDictionary:_data];
        vc.blcok = ^(NSDictionary *dic){
            weakSelf.data = dic;
            [weakSelf.tableV reloadData];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }else if (tap.view.tag == 2){//教育经历
        EduViewController *vc = [EduViewController new];
        vc.data = [NSMutableDictionary dictionaryWithDictionary:_data];
        vc.blcok = ^(NSDictionary *dic){
            weakSelf.data = dic;
            [weakSelf.tableV reloadData];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }else if (tap.view.tag == 3){//期望工作
        HopViewController *vc = [HopViewController new];
        vc.data = [NSMutableDictionary dictionaryWithDictionary:_data];
        vc.block = ^(NSDictionary *dic){
            weakSelf.data = dic;
            [weakSelf.tableV reloadData];
        };
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - @s
-(NSString *)getSex
{
    if ([_data[@"sex"] integerValue] == 1) {
        return @"男";
    }else if ([_data[@"sex"] integerValue] == 2){
        return @"女";
    }
    return @"";
}

-(NSString *)getEducation
{
    switch ([_data[@"education"] integerValue]) {
        case 0:
            return @"大专";
            break;
        case 1:
            return @"本科";
            break;
        case 2:
            return @"硕士";
            break;
        case 3:
            return @"博士";
            break;
        case 4:
            return @"其他";
            break;
        default:
            return @"未知";
            break;
    }
}

-(NSString *)getWorkingLife
{
    if ([_data[@"workingLife"] integerValue] == 0) {
        return @"应届毕业生";
    }else if ([_data[@"workingLife"] integerValue] == 11){
        return @"10年以上";
    }else{
        return [NSString stringWithFormat:@"%@年",_data[@"workingLife"]];
    }
}

#pragma mark - request
-(void)getDetail
{
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/user/getResumeInfo" Dic:@{@"id":[Storage getToken]} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {
                    _data = responseDic[@"data"];
                    [_tableV reloadData];
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
            });
        }];
    });
}

@end
